define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
		index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/transfer/index' + location.search,
                    del_url: 'stock/transfer/del',
                    table: 'stock_transfer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {checkbox: true},
						{field: 'idcode', title: __('票据编号'), operate: 'LIKE'},
                        {field: 'from_nickname', title: __('持票人'), operate: 'LIKE'},
						{field: 'from_username', title: __('持票人工号'), operate: 'LIKE'},
						{field: 'from_depart', title: __('持票人单位'), searchList: $.getJSON("stock/transfer/depart")},
						{field: 'money', title: __('转让金额'), operate:'BETWEEN'},
						{field: 'to_nickname', title: __('受让人'), operate: 'LIKE'},
						{field: 'to_username', title: __('受让人工号'), operate: 'LIKE'},
                        {field: 'to_depart', title: __('受让人单位'), searchList: $.getJSON("stock/transfer/depart")},
                        {field: 'status', title: __("Status"), searchList: {"0":__('未审核'),"1":__('已审核'),"2":__('已受让')}, formatter: Table.api.formatter.status},
						{field: 'checktime', title: __('审核时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
						{field: 'accepttime', title: __('受让时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
						{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons:[], formatter: function (value, row, index) {
								var buttons = [];
                                if(row.status == 0){
									if(Config.admin.groupid == 2)
									{
										buttons.push({
											name: 'review',
											title: __('审核'),
											text:__('审核'),
											classname: 'btn btn-xs btn-warning btn-dialog',
											url: 'stock/transfer/review',
											extend: 'data-area=\'["50%","45%"]\''
										});
									}
									if(row.type != 2)
									{
										buttons.push(Table.button.del);
									}
                                }else if(row.status == 1){
									if(row.type == 2)
									{
										buttons.push({
											name: 'transfer',
											title: __('受让'),
											text:__('受让'),
											classname: 'btn btn-xs btn-warning btn-ajax',
											confirm: '确定要受让吗？',
											refresh:true,
											url: 'stock/transfer/accept'
										});
									}
									if(Config.admin.groupid != 3)
									{
										buttons.push(Table.button.del);
									}
								}else if(row.status == 2){
									if(Config.admin.groupid != 3)
									{
										buttons.push(Table.button.del);
									}
								}
								return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
                            }}
                    ]
                ]
            });

			// 切换分类
            $(document).on("click", ".nav-tabs li a", function () {
                $(".nav-tabs li").removeClass("active");
                $(this).parent().addClass("active");
                table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
                return false;
            });
			
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
		review: function () {
			Controller.api.bindevent();
		},
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
